local SFTRConst_40 = Font ("SF Transrobotics", 40)
local ZEKConst_20 = Font ("Zekton Bold", 20)
local ZEKConst_18 = Font ("Zekton Bold", 18)

local CSDConst_70 = Font ("csd", 70, nil, false, false)
local HL2MPConst_70 = Font ("HL2MP", 70, nil, false, false)

local TabNames = {
	"PRIMARY",
	"SECONDARY",
	"SUPPORT",
	"GRENADES"
}

--main window thing (this is the 'frame')

local PANEL = {}

function PANEL:Init ()
	self.Tabs = {}
	for k,v in pairs (GAMEMODE.CombatWeapons) do
		local tab = vgui.Create ("inc_weaponmenu_tab", self)
		tab:SetWeaponType (k)
		table.insert (self.Tabs, tab)
	end
	self.Body = vgui.Create ("inc_weaponmenu_body", self)
	self.WeaponArrays = {}
	for wpntype,_ in pairs (GAMEMODE.CombatWeapons) do
		local array = vgui.Create ("inc_weaponmenu_weaponarray", self)
		array:SetWeaponType (wpntype)
		if wpntype != 1 then array:SetVisible (false) end
		self.WeaponArrays[wpntype] = array
	end
end

function PANEL:PerformLayout ()
	self:SetSize (635, 400)
	self:SetPos (ScrW() / 2 - self:GetWide() / 2, ScrH() / 2 - self:GetTall() / 2)
	y = 5
	for _,pnl in pairs (self.Tabs) do
		pnl:SetPos (0, y)
		y = y + 50
	end
	self.Body:SetSize (395, 400)
	self.Body:SetPos (224, 0)
	
	for k,v in pairs (self.WeaponArrays) do
		v:SetSize (385, 390)
		v:SetPos (229, 5)
	end
end

function PANEL:SetWeaponTypeVisible (wpntype)
	for num,pnl in pairs (self.WeaponArrays) do
		pnl:SetVisible (num == wpntype)
	end
end

function PANEL:GetWeaponType ()
	for k,v in pairs (self.WeaponArrays) do
		if v:IsVisible() then return k end
	end
end

vgui.Register ("inc_weaponmenu", PANEL)

PANEL = {}

function PANEL:Init ()
	
end

function PANEL:SetWeaponType (num)
	self.wpntype = num
end

function PANEL:Paint ()
	local quad = Quad( 0, 0, self:GetWide(), 20, self:GetTall(), self:GetWide() - 20 )
	DrawQuad (quad, HUD_BOX_COLOUR, HUD_TEXT_COLOUR)
	--surface.SetDrawColor (HUD_TEXT_COLOUR.r, HUD_TEXT_COLOUR.g, HUD_TEXT_COLOUR.b, HUD_TEXT_COLOUR.a)
	--surface.DrawLine (quad[4].x - 1, quad[4].y, quad[1].x - 1, quad[1].y)
	--SFTRConst_40
	local col = HUD_TEXT_OUTLINE_COLOUR
	if self:GetParent():GetWeaponType() == self.wpntype then
		col = HUD_TEXT_COLOUR
	end
	draw.SimpleText (TabNames[self.wpntype], SFTRConst_40, self:GetWide() - 10, 5, col, 2)
	return true
end

function PANEL:PerformLayout ()
	self:SetSize (225, 50)
end

function PANEL:DoClick ()
	self:GetParent():SetWeaponTypeVisible (self.wpntype)
end

vgui.Register ("inc_weaponmenu_tab", PANEL, "Button")

PANEL = {}

function PANEL:Paint ()
	surface.SetDrawColor (HUD_BOX_COLOUR.r, HUD_BOX_COLOUR.g, HUD_BOX_COLOUR.b, HUD_BOX_COLOUR.a)
	surface.DrawRect (0,0,self:GetWide(),self:GetTall())
	surface.SetDrawColor (HUD_TEXT_COLOUR.r, HUD_TEXT_COLOUR.g, HUD_TEXT_COLOUR.b, HUD_TEXT_COLOUR.a)
	surface.DrawOutlinedRect (-1,0,self:GetWide()+1,self:GetTall())
	local y = self:GetParent().Tabs[self:GetParent():GetWeaponType()].Y
	surface.SetDrawColor (HUD_TEXT_COLOUR.r, HUD_TEXT_COLOUR.g, HUD_TEXT_COLOUR.b, HUD_TEXT_COLOUR.a)
	surface.DrawLine (0,0,0,y)
	local y2 = y + 50
	surface.DrawLine (0,y2,0,self:GetTall())
end

vgui.Register ("inc_weaponmenu_body", PANEL)

PANEL = {}

function PANEL:SetWeaponType (num)
	printd ("wpntype: "..num)
	self.wpntype = num
	self:GenerateWeaponButtons()
end

function PANEL:GenerateWeaponButtons ()
	printd ("generating ..  .. ")
	self.Buttons = {}
	for k,v in pairs (GAMEMODE.CombatWeapons[self.wpntype]) do
		printd ("making button "..k)
		local btn = vgui.Create ("inc_weaponmenu_weaponbutton", self)
		btn:SetWeapon (self.wpntype, k)
		table.insert (self.Buttons, btn)
	end
	self:InvalidateLayout ()
end

function PANEL:Paint ()
	--surface.SetDrawColor (HUD_TEXT_COLOUR.r, HUD_TEXT_COLOUR.g, HUD_TEXT_COLOUR.b, HUD_TEXT_COLOUR.a)
	--surface.DrawOutlinedRect (0,0,self:GetWide(),self:GetTall())
end

function PANEL:PerformLayout ()
	local x = 1
	local y = 1
	for k,v in pairs (self.Buttons) do
		v:SetPos (5 + (x-1)*95, 5 + (y-1)*75)
		if x >= 4 then x = 1; y = y + 1 else x = x + 1 end
	end
end

vgui.Register ("inc_weaponmenu_weaponarray", PANEL)

PANEL = {}

function PANEL:SetWeapon (wpntype, wpnid)
	self.wpntype = wpntype
	self.wpnid = wpnid
	self.weaponTable = GAMEMODE.CombatWeapons[wpntype][wpnid]
	if self.weaponTable.PurchasePerAmmo then
		self.PurchasePerAmmo = true
		self.weaponTable.Units = 0
		printd ("purchaseperammo")
	else
		printd ("license")
	end
end

function PANEL:OnCursorEntered ()
	self.mouseOver = true
end

function PANEL:OnCursorExited ()
	self.mouseOver = false
	if self.showingInfobox then
		self:HideInfobox()
	end
end

function PANEL:DoClick()
	--RunConsoleCommand ("inc_purchaseweapon", self.wpntype, self.wpnid)
	if not self.showingInfobox then
		self:ShowInfobox()
		if self.weaponTable.Licensed and not self.PurchasePerAmmo then
			RunConsoleCommand ("inc_purchaseweapon", self.wpntype, self.wpnid)
		end
	end
end
function PANEL:DoRightClick()
	if self.showingInfobox then
		RunConsoleCommand ("inc_purchaseweapon", self.wpntype, self.wpnid)
	end
end

function PANEL:ShowInfobox()
	if self.infobox then
		self.infobox:SetVisible (true)
	else
		self.infobox = vgui.Create ("inc_weaponmenu_infobox")
		local x,y = self:LocalToScreen (0,0)
		self.infobox:PositionRelativeTo (x, y)
		self.infobox:SetButton (self)
	end
	self.showingInfobox = true
end

function PANEL:HideInfobox()
	if self.infobox then
		self.infobox:SetVisible (false)
	end
	self.showingInfobox = false
end

function PANEL:Paint ()
	surface.SetDrawColor (HUD_OUTLINE_COLOUR_SOLID.r, HUD_OUTLINE_COLOUR_SOLID.g, HUD_OUTLINE_COLOUR_SOLID.b, HUD_OUTLINE_COLOUR_SOLID.a)
	surface.DrawRect (0,0,self:GetWide(),self:GetTall())
	
	if self.mouseOver then
		surface.SetDrawColor (HUD_LINE_COLOUR.r, HUD_LINE_COLOUR.g, HUD_LINE_COLOUR.b, HUD_LINE_COLOUR.a)
		--surface.DrawOutlinedRect (0,0,self:GetWide(),self:GetTall())
		surface.DrawLine (0,0,0,self:GetTall())
		surface.DrawLine (0,self:GetTall()-1,self:GetWide()-1,self:GetTall()-1)
		surface.DrawLine (self:GetWide()-1,0,self:GetWide()-1,self:GetTall()-1)
		if not self.showingInfobox then
			surface.DrawLine (0,0,self:GetWide(),0)
		end
	end
	
	local col = HUD_BOX_COLOUR
	local font = CSDConst_70
	
	if self.weaponTable.IconFont == "HL2MPTypeDeath" then
		font = HL2MPConst_70
	end
	
	if self.weaponTable.Licensed then
		col = HUD_TEXT_COLOUR
		if self.weaponTable.Active then
			draw.SimpleText ("ACTIVE", ZEKConst_20, self:GetWide() * 0.5, self:GetTall() - 25, col, 1)
		end
	elseif ( !self.PurchasePerAmmo ) then
		draw.SimpleText (self.weaponTable.Cost, ZEKConst_20, self:GetWide() * 0.5, self:GetTall() - 25, col, 1)
	end
	
	if (self.PurchasePerAmmo and self.weaponTable.Units > 0) then
		col = HUD_TEXT_COLOUR
	end
	
	if self.PurchasePerAmmo then
		draw.SimpleText (self.weaponTable.IconLetter, font, self:GetWide() * 0.5, 5, col, 1)
		draw.SimpleText (self.weaponTable.Cost .. "x"..self.weaponTable.Units, ZEKConst_20, self:GetWide() * 0.5, self:GetTall() - 25, col, TEXT_ALIGN_CENTER )
	else
		draw.SimpleText (self.weaponTable.IconLetter, font, self:GetWide() * 0.5, 5, col, 1)
	end
	
	return true
end

function PANEL:PerformLayout ()
	self:SetSize (90, 70)
end

vgui.Register ("inc_weaponmenu_weaponbutton", PANEL, "Button")

PANEL = {}

PANEL.W = 220
PANEL.H = 50

function PANEL:Init ()
	
end

function PANEL:SetButton (btn)
	self.button = btn
end

function PANEL:Paint ()
	--rects
	surface.SetDrawColor (HUD_OUTLINE_COLOUR_SOLID.r, HUD_OUTLINE_COLOUR_SOLID.g, HUD_OUTLINE_COLOUR_SOLID.b, HUD_OUTLINE_COLOUR_SOLID.a)
	surface.DrawRect (5,0,85,self:GetTall())
	surface.DrawRect (90,0,self.W,self:GetTall()-5)
	
	--lines
	surface.SetDrawColor (HUD_LINE_COLOUR.r, HUD_LINE_COLOUR.g, HUD_LINE_COLOUR.b, HUD_LINE_COLOUR.a)
	surface.DrawLine (5,0,5,self:GetTall())
	surface.DrawLine (-1,self:GetTall()-1,5,self:GetTall()-1)
	surface.DrawLine (5,0,self:GetWide()-1,0)
	surface.DrawLine (self:GetWide()-1,0,self:GetWide()-1,self:GetTall()-6)
	surface.DrawLine (self:GetWide()-1,self:GetTall()-6,90,self:GetTall()-6)
	surface.DrawLine (89,self:GetTall()-6,89,self:GetTall())
	
	--text
	draw.SimpleText (self.button.weaponTable.PrintName, ZEKConst_20, 8, 0, HUD_TEXT_COLOUR)
	
	--description - wordwrap party time
	local lineLength = self.W - 17
	local words = string.Explode (" ", self.button.weaponTable.Description or "No description available")
	local str = ""
	local line = 1
	
	surface.SetFont (ZEKConst_20)
	
	for k,v in pairs (words) do
		local newstr = str.." "..v
		if surface.GetTextSize (newstr) > lineLength then
			draw.SimpleText (str, ZEKConst_20, 12, 20 + 15*(line-1), HUD_TEXT_COLOUR)
			line = line + 1
			str = v
		else
			str = newstr
		end
	end
	draw.SimpleText (str, ZEKConst_20, 12, 20 + 15*(line-1), HUD_TEXT_COLOUR)
	
	if not self.button.weaponTable.Active or self.button.PurchasePerAmmo then
		local selectText = "purchase"
		if self.button.weaponTable.Cost > GAMEMODE:GetCredits() then
			draw.SimpleText ("You cannot afford this", ZEKConst_20, self:GetWide()-5, 40 + 15*(line-1), HUD_TEXT_COLOUR, TEXT_ALIGN_RIGHT)
		else
			local text = ""
			if self.button.PurchasePerAmmo then text = " ONE" end
			draw.SimpleText ("RIGHT CLICK to "..selectText..text, ZEKConst_20, self:GetWide()-5, 40 + 15*(line-1), HUD_TEXT_COLOUR, TEXT_ALIGN_RIGHT)
		end
	end
	
	if not self.sized then
		self.H = 70 + 15*(line-1)
		self:PositionRelativeTo (self.relx, self.rely)
		self:InvalidateLayout()
	end
end

function PANEL:PositionRelativeTo (x,y)
	self.relx = x
	self.rely = y
	self:SetPos (x, y - self.H)
end

function PANEL:PerformLayout ()
	self:SetSize (self.W, self.H)
end

vgui.Register ("inc_weaponmenu_infobox", PANEL)

function GM:CreateWeaponMenu()
	self.WeaponMenu = vgui.Create ("inc_weaponmenu")
	self.WeaponMenu:SetVisible (false)
end
GM:AddHook ("InitPostEntity", "CreateWeaponMenu")

function GM:OpenWeaponsMenu()
	if !self.WeaponMenu then return end
	self.WeaponMenu:SetVisible (not self.WeaponMenu:IsVisible())
	gui.EnableScreenClicker (self.WeaponMenu:IsVisible())
end
GM:AddHook ("OnContextMenuOpen", "OpenWeaponsMenu")